package com.ruoyi.evaluate.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.evaluate.mapper.EvaluateNormMapper;
import com.ruoyi.evaluate.mapper.EvaluateResultsMapper;
import com.ruoyi.evaluate.mapper.EvaluateScoreMapper;
import com.ruoyi.base.domain.BaseStudent;
import com.ruoyi.base.mapper.BaseStudentMapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.evaluate.domain.EvaluateNorm;
import com.ruoyi.evaluate.domain.EvaluateResults;
import com.ruoyi.evaluate.domain.EvaluateScore;
import com.ruoyi.evaluate.service.IEvaluateNormService;
import com.ruoyi.evaluate.service.IEvaluateScoreService;

/**
 * 评价分数Service业务层处理
 * 
 * @author yepanpan
 * @date 2020-11-09
 */
@Service
public class EvaluateScoreServiceImpl implements IEvaluateScoreService 
{
    @Autowired
    private EvaluateScoreMapper evaluateScoreMapper;
    @Autowired
    private EvaluateResultsMapper evaluateResultsMapper;
    @Autowired
    private IEvaluateNormService evaluateNormService;
    @Autowired
    private BaseStudentMapper baseStudentMapper;

    /**
     * 查询评价分数
     * 
     * @param id 评价分数ID
     * @return 评价分数
     */
    @Override
    public EvaluateScore selectEvaluateScoreById(Long id)
    {
        return evaluateScoreMapper.selectEvaluateScoreById(id);
    }

    /**
     * 查询评价分数列表
     * 
     * @param evaluateScore 评价分数
     * @return 评价分数
     */
    @Override
    public List<EvaluateScore> selectEvaluateScoreList(EvaluateScore evaluateScore)
    {
        return evaluateScoreMapper.selectEvaluateScoreList(evaluateScore);
    }

    /**
     * 新增评价分数
     * 
     * @param evaluateScore 评价分数
     * @return 结果
     */
    @Override
    public int insertEvaluateScore(EvaluateScore evaluateScore)
    {
        return evaluateScoreMapper.insertEvaluateScore(evaluateScore);
    }

    /**
     * 修改评价分数
     * 
     * @param evaluateScore 评价分数
     * @return 结果
     */
    @Override
    public int updateEvaluateScore(EvaluateScore evaluateScore)
    {
        return evaluateScoreMapper.updateEvaluateScore(evaluateScore);
    }

    /**
     * 批量删除评价分数
     * 
     * @param ids 需要删除的评价分数ID
     * @return 结果
     */
    @Override
    public int deleteEvaluateScoreByIds(Long[] ids)
    {
        return evaluateScoreMapper.deleteEvaluateScoreByIds(ids);
    }

    /**
     * 删除评价分数信息
     * 
     * @param id 评价分数ID
     * @return 结果
     */
    @Override
    public int deleteEvaluateScoreById(Long id)
    {
        return evaluateScoreMapper.deleteEvaluateScoreById(id);
    }
     
    /**
     * 保存评价分数
     * 
     * @param evaluateScore 评价分数
     * @return 结果
     */
    @Override
    @Transactional
    public int saveEvaluateScore(Map<String, Object> data) {
    	int count = 0;
    	float total = 0;
    	SysUser user = SecurityUtils.getLoginUser().getUser();
    	Long teahcerId = user.getUserId();
		Long stuId = Long.parseLong(data.get("stuId").toString());
		BaseStudent stu = baseStudentMapper.selectBaseStudentById(stuId);
		Long semId = Long.parseLong(data.get("semId").toString());
    	List<EvaluateNorm> list = evaluateNormService.getEvaluateNorms();
    	Date d = new Date();
    	//各指标分数
    	for(EvaluateNorm norm : list) {
    
    		float score = Float.valueOf(data.get("norm"+norm.getId()).toString());
    		if((norm.getMin() != null && norm.getMin().floatValue() > score) || (norm.getMax() != null && norm.getMax().floatValue() < score)){
    			throw new CustomException(norm.getTitle() + " 指标的分值超出允许的范围");
    		}
    		
    		EvaluateScore vo = new EvaluateScore();
    		vo.setTeacherId(teahcerId);
    		vo.setNormId(norm.getId());
    		vo.setSemId(semId);
    		vo.setStuId(stuId);
    		vo.setCreateTime(d);
    		vo.setClassId(stu.getClassId());
    		EvaluateScore old = evaluateScoreMapper.findEvaluateScore(vo);
    		if(old == null) {
    			vo.setScore(BigDecimal.valueOf(score));
    			evaluateScoreMapper.insertEvaluateScore(vo);
    		}else {
    			old.setScore(BigDecimal.valueOf(score));
    			evaluateScoreMapper.updateEvaluateScore(old);
    		}
    		total += score;
    		count ++;
    	}
    	EvaluateResults results = new EvaluateResults();
    	results.setSemId(semId);
    	results.setStuId(stuId);
    	results.setEvaluateScore(new BigDecimal(total));
    	results.setRemark(data.getOrDefault("remark","").toString());
    	evaluateResultsMapper.updateEvaluateScore(results);
    	return count;
    }
    
}
